package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.sql.DataSource;

public class TxAdvice {
    private DataSource dataSource;
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Object transactionManager(ProceedingJoinPoint pjt) throws Throwable {
        DataSourceTransactionManager ds = new DataSourceTransactionManager(dataSource);
        // 事务定义
        TransactionDefinition de = new DefaultTransactionDefinition();
        // 事务状态
        TransactionStatus st = ds.getTransaction(de);

        pjt.proceed();

        ds.commit(st);
        return pjt;
    }
}
